package com.wangsh.splider.common.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wangsh.splider.common.pojo.ApiResponse;
import com.wangsh.splider.common.pojo.ApiResponseEnum;

/**
 * faceBook的常用类
 * @author TeaBig
 */
@Component("faceBookUtil")
public class FaceBookUtil
{
	/* HttpUtil的工具类 */
	@Resource
	private HttpUtil httpUtil;
	/* jsoupUtil的工具类 */
	@Resource
	private JsoupUtil jsoupUtil ;
	/* domain */
	private String domain = ".facebook.com" ; 
	
	private JSONObject headerCommonJSON = new JSONObject();
	
	public FaceBookUtil()
	{
		/* 存储一些公共的请求头 */
		headerCommonJSON.put("accept-language", "zh-CN,zh;q=0.9");
		headerCommonJSON.put("commonHeader","facebook");
		
		headerCommonJSON.put("referer", "https://www.facebook.com/");
		headerCommonJSON.put("origin", "https://www.facebook.com/");
	}
	
	/**
	 * 访问facebook的首页
	 * @param resultMap 存储结果而用
	 * @return
{
	"code":"0",
	"info":"",
	"data":
	{
		"cookieTarget":"",
		"response":""
	}
}
	 */
	public JSONObject main(JSONObject reqJSON)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====main====");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> dataOne = new HashMap<String, Object>();
		apiResponse.setDataOne(dataOne);
		/* 获取请求头和请求体 */
		JSONObject headerJSON = reqJSON.getJSONObject("header"); 
		JSONObject requestJSON = reqJSON.getJSONObject("request"); 
		try
		{
			String urlStr = ConstatFinalUtil.URLS_JSON.getString("facebook_main"); 
			/* ====请求头 ==== */
			headerJSON.putAll(headerCommonJSON);
			Map<String, String> headerMap = ConstatFinalUtil.jsonToMap(headerJSON);
			/* 生成json的语言 */
			headerMap.put(ConstatFinalUtil.REQ_HEADER_IGNORE + "domain", domain);
			/* 随机的存储Cookie和userAgent */
			ConstatFinalUtil.headerRandom(headerMap);
			//headerMap.put("", "");
			/* ====请求体 ==== */
			Map<String, String> paramsMap = ConstatFinalUtil.jsonToMap(requestJSON);
			/* ====响应体 ==== */
			Map<String, String> responseMap = new HashMap<String, String>();
			
			/* 请求网址 */
			String response = this.httpUtil.methodPost(urlStr, headerMap, paramsMap, responseMap);
			//String response = this.httpUtil.broswerRequest(urlStr, "post", headerMap, paramsMap, responseMap);
			
			/* 将完整的请求头存储下来 */
			headerJSON.putAll(headerMap);
			requestJSON.putAll(paramsMap);
			
			/* 把Map变成Json */
			JSONObject responseJSONMap = new JSONObject() ;
			responseJSONMap.putAll(responseMap);
			dataOne.put("responseMap", responseJSONMap);
			
			/* 存储信息 */
			dataOne.put("cookie", responseMap.get("cookieTarget"));
			dataOne.put("cookieArr", responseMap.get("cookieArr"));
			dataOne.put("response", response);
			/* 解析网页上的信息 */
			JSONObject infoJSON = this.info(response);
			dataOne.put("infoJSON", infoJSON);
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("报错了",e);
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse.toJSON() ; 
	}
	
	/**
	 * 获取网页上所有的登陆信息
	 * @return
	 */
	private JSONObject info(String response)
	{
		JSONObject resultJSON = new JSONObject();
		/* 解析HTML */
		Document responseDocu = Jsoup.parse(response);
		/* 获取登陆的提交信息 */
		Element loginEle = responseDocu.getElementById("login_form");
		if(loginEle != null)
		{
			infoLoginUtil(resultJSON, loginEle);
		}
		
		/* 获取登陆的提交信息 */
		loginEle = responseDocu.getElementById("u_0_9");
		if(loginEle != null && resultJSON.size() == 0 )
		{
			infoLoginUtil(resultJSON, loginEle);
		}
		
		/* 获取登陆的提交信息 */
		loginEle = responseDocu.getElementById("u_0_a");
		if(loginEle != null && resultJSON.size() == 0 )
		{
			infoLoginUtil(resultJSON, loginEle);
		}
		
		/* 获取登陆的提交信息 */
		loginEle = responseDocu.getElementById("u_c_0");
		if(loginEle != null && resultJSON.size() == 0 )
		{
			infoLoginUtil(resultJSON, loginEle);
		}
		
		/* 获取登陆的提交信息 */
		loginEle = responseDocu.getElementById("u_0_c");
		if(loginEle != null && resultJSON.size() == 0 )
		{
			infoLoginUtil(resultJSON, loginEle);
		}
		return resultJSON ; 
	}

	/**
	 * 收集登陆信息的工具类
	 * @param resultJSON
	 * @param loginEle
	 */
	private void infoLoginUtil(JSONObject resultJSON, Element loginEle)
	{
		JSONObject loginJSON = new JSONObject();
		/* 提交网址 */
		String action = loginEle.attr("action").trim();
		if(!action.startsWith(ConstatFinalUtil.URLS_JSON.getString("facebook_main")))
		{
			action = ConstatFinalUtil.URLS_JSON.getString("facebook_main") + "/" + action ; 
		}
		loginJSON.put("action", action);
		/* 提交的参数信息 */
		JSONArray paramsArr = new JSONArray();
		/* 获取所有的提交信息 */
		Elements paramsEles = loginEle.getElementsByAttributeValue("type", "hidden");
		for (Iterator iterator = paramsEles.iterator(); iterator.hasNext();)
		{
			Element paramsEle = (Element) iterator.next();
			String name = paramsEle.attr("name");
			String value = paramsEle.attr("value");
			
			JSONObject paramsJSON = new JSONObject();
			paramsJSON.put("name", name);
			paramsJSON.put("value", value);
			
			paramsArr.add(paramsJSON);
		}
		/* 提交的参数 */
		loginJSON.put("paramsArr", paramsArr);
		
		/* 存储信息 */
		resultJSON.put("login", loginJSON);
	}
	
	/**
	 * 登陆
	 * @param resultMap
	 * @return
{
	"code":"0",
	"info":"",
	"data":
	{
		"cookieTarget":"",
		"response":""
	}
}
	 */
	public JSONObject login(String urlStr, JSONObject reqJSON)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====login====上行信息:{}",reqJSON.toJSONString());
		/* 登陆字符串 */
		String loginStr = "c_user=" ; 
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> dataOne = new HashMap<String, Object>();
		apiResponse.setDataOne(dataOne);
		/* 获取请求头和请求体 */
		JSONObject headerJSON = reqJSON.getJSONObject("header"); 
		JSONObject requestJSON = reqJSON.getJSONObject("request"); 
		try
		{
			/* ====请求头 ==== */
			headerJSON.putAll(headerCommonJSON);
			Map<String, String> headerMap = ConstatFinalUtil.jsonToMap(headerJSON);
			/* 生成json的语言 */
			headerMap.put(ConstatFinalUtil.REQ_HEADER_IGNORE + "domain", domain);
			/* 随机的存储Cookie和userAgent */
			ConstatFinalUtil.headerRandom(headerMap);
			
			//headerMap.put("", "");
			/* ====请求体 ==== */
			Map<String, String> paramsMap = ConstatFinalUtil.jsonToMap(requestJSON);
			
			/* ====响应体 ==== */
			Map<String, String> responseMap = new HashMap<String, String>();
			
			/* 请求网址 */
			String response = this.httpUtil.methodPost(urlStr, headerMap, paramsMap, responseMap);
			//String response = this.httpUtil.broswerRequest(urlStr, "post", headerMap, paramsMap, responseMap);
			
			/* 将完整的请求头存储下来 */
			headerJSON.putAll(headerMap);
			requestJSON.putAll(paramsMap);
			
			/* 把Map变成Json */
			JSONObject responseJSONMap = new JSONObject() ;
			responseJSONMap.putAll(responseMap);
			dataOne.put("responseMap", responseJSONMap);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			/* 获取最终的Cookie */
			String cookieTarget = responseMap.get("cookieTarget") ; 
			if(cookieTarget != null && cookieTarget.indexOf(loginStr) != -1)
			{
				/* 登陆成功 */
				apiResponse.setCode(ApiResponseEnum.INFO_LOGIN_SUCCES.getStatus());
			}else
			{
				/* 登陆以后的网址 */
				String location = responseJSONMap.getString("Location");
				if(location != null)
				{
					JSONObject resultJSON = this.accessUrl(responseJSONMap);
					/* 登陆以后的网址 */
					JSONObject dataResJSON = (JSONObject) resultJSON.get("data");
					response = dataResJSON.getString("response");
				}
				/* 登陆失败 */
				ApiResponseEnum apiResponseEnum = this.loginErrinfo(response);
				apiResponse.setCode(apiResponseEnum.getStatus());
			}
			
			dataOne.put("cookie", cookieTarget);
			dataOne.put("cookieArr", responseMap.get("cookieArr"));
			dataOne.put("response", response);
			

			/* 获取到c_user的值 */
			String key = "c_user" ; 
			Map<String, String> cookieMap = this.httpUtil.cookieSplit(cookieTarget);
			String value = cookieMap.get(key);
			if(value != null && !"".equalsIgnoreCase(value))
			{
				/* 存储到JSON中 */
				dataOne.put("usersId", value);
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("报错了",e);
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse.toJSON() ; 
	}
	
	/**
	 * 确认错误信息
	 * 	邮箱不存在:找不到与输入的邮箱或手机号对应的帐户。请<a href=\"/r.php\">注册帐户</a>。
	 * 	密码错误:密码有误
	 * @return
		"usersNotExist"
	 */
	private ApiResponseEnum loginErrinfo(String response)
	{
		/* 解析HTML */
		Document responseDocu = Jsoup.parse(response);
		String infoStart = "\"__markup_a588f507_0_0\"," ; 
		String infoEnd = ",1],[\"__markup_3310c079_0_0";
		/* 提示信息 */
		String info = "" ; 
		/* 判断错误信息 */
		if(response.indexOf(infoStart) != -1 && response.indexOf(infoEnd) != -1)
		{
			/* 截取信息 */
			info = response.substring(response.indexOf(infoStart) + infoStart.length(), response.indexOf(infoEnd));
		}
		if("".equalsIgnoreCase(info) || info == null)
		{
			/* 您输入的是旧密码 */
			Element errELE = responseDocu.getElementById("error_box");
			if(errELE != null)
			{
				/* 说明找到了 */
				Element childOneEle = errELE.child(0);
				String text = childOneEle.text() ; 
				if(text.indexOf("旧密码") != -1)
				{
					/* 密码正确,但是输入的是旧密码 */
					return ApiResponseEnum.INFO_SOUPASS_SUCCESS ; 
				}
			}
		}
		//ConstatFinalUtil.SYS_LOGGER.info("错误信息:{}",info);
		JSONObject infoJSON = (JSONObject) JSON.parse(info);
		if(infoJSON != null)
		{
			//ConstatFinalUtil.SYS_LOGGER.info("错误信息json:{}",infoJSON.toJSONString());
			String infoJSONStr = infoJSON.toJSONString();
			if(infoJSONStr.indexOf("注册帐户") != -1)
			{
				/* 邮箱不存在 */
				return ApiResponseEnum.INFO_EMAIL_NOT_EXISTS ; 
			}else if(infoJSONStr.indexOf("密码有误") != -1)
			{
				/* 密码出错 */
				return ApiResponseEnum.INFO_PASS_INCORRECT ; 
			}
		}
		/* 默认服务器报错 */
		return ApiResponseEnum.STATUS_FAILED ; 
	}
	
	/**
	 * 登陆后访问首页
	 * @param resultMap 存储结果而用
	 * @return
{
	"code":"0",
	"info":"",
	"data":
	{
		"cookieTarget":"",
		"response":""
	}
}
	 */
	public JSONObject mainAfterLogin(JSONObject reqJSON)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====mainAfterLogin====");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> dataOne = new HashMap<String, Object>();
		apiResponse.setDataOne(dataOne);
		/* 获取请求头和请求体 */
		JSONObject headerJSON = reqJSON.getJSONObject("header"); 
		JSONObject requestJSON = reqJSON.getJSONObject("request"); 
		try
		{
			String urlStr = ConstatFinalUtil.URLS_JSON.getString("facebook_main"); 
			/* ====请求头 ==== */
			headerJSON.putAll(headerCommonJSON);
			Map<String, String> headerMap = ConstatFinalUtil.jsonToMap(headerJSON);
			/* 生成json的语言 */
			headerMap.put(ConstatFinalUtil.REQ_HEADER_IGNORE + "domain", domain);
			/* 随机的存储Cookie和userAgent */
			ConstatFinalUtil.headerRandom(headerMap);
			//headerMap.put("", "");
			/* ====请求体 ==== */
			Map<String, String> paramsMap = ConstatFinalUtil.jsonToMap(requestJSON);
			/* ====响应体 ==== */
			Map<String, String> responseMap = new HashMap<String, String>();
			
			/* 请求网址 */
			String response = this.httpUtil.methodGet(urlStr, headerMap, paramsMap, responseMap);
			//String response = this.httpUtil.broswerRequest(urlStr, "get", headerMap, paramsMap, responseMap);
			/* Cookie最终的字符串 */
			String cookieTarget = responseMap.get("cookieTarget") ;
			dataOne.put("cookie", cookieTarget);
			/* 获取到Json字符串 */
			String cookieArr = responseMap.get("cookieArr");
			dataOne.put("cookieArr", cookieArr);
			dataOne.put("response", response);
			/* 把Map变成Json */
			JSONObject responseMapJSON = new JSONObject() ;
			responseMapJSON.putAll(responseMap);
			dataOne.put("responseMap", responseMapJSON);
			
			/* 将完整的请求头存储下来 */
			headerJSON.putAll(headerMap);
			requestJSON.putAll(paramsMap);
			
			
			/* 请求指定网站的内容 */
			String location = responseMapJSON.getString("Location");
			if(location != null)
			{
				JSONObject accessResultJSON = this.accessUrl(responseMapJSON);
				/* 登陆以后的网址 */
				JSONObject dataResJSON = (JSONObject) accessResultJSON.get("data");
				response = dataResJSON.getString("response");
				dataOne.put("response", dataResJSON.get("response"));
				
				/* Cookie最终的字符串 */
				cookieTarget = dataResJSON.getString("cookieTarget") ;
				if(cookieTarget != null && !"".equalsIgnoreCase(cookieTarget))
				{
					dataOne.put("cookie", cookieTarget);
					/* 获取到Json字符串 */
					cookieArr = dataResJSON.getString("cookieArr");
					dataOne.put("cookieArr", cookieArr);
				}
			}
			
			/* 必须Response有值才可以 */
			if(!"".equalsIgnoreCase(response) && response != null)
			{
				/* 处理登陆信息 */
				boolean loginFlag = verifyLogin(response);
				if(loginFlag)
				{
					/* Cookie好使 */
					apiResponse.setCode(ApiResponseEnum.INFO_COOKIE_SUCCESS.getStatus());
				}else
				{
					/* Cookie不好使 */
					apiResponse.setCode(ApiResponseEnum.INFO_COOKIE_FAILED.getStatus());
				}
			}
			
			/* 获取到c_user的值 */
			String key = "c_user" ; 
			Map<String, String> cookieMap = this.httpUtil.cookieSplit(cookieTarget);
			String value = cookieMap.get(key);
			if(value != null && !"".equalsIgnoreCase(value))
			{
				/* 存储到JSON中 */
				dataOne.put("usersId", value);
			}
			
			/* 解析网页上的信息 */
			JSONObject infoJSON = this.infoUtil(response);
			dataOne.put("infoJSON", infoJSON);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("报错了",e);
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse.toJSON() ; 
	}
	
	/**
	 * 获取登陆以后,首页上的信息
	 * 登陆后的页面,所有链接方式都通用
	 * @return
	 */
	private JSONObject infoUtil(String response)
	{
		JSONObject resultJSON = new JSONObject();
		/* 解析HTML */
		Document document = Jsoup.parse(response);
		/* 获取个人主页的链接地址 */
		Element perMainEle = document.getElementById("u_0_a");
		if(perMainEle != null)
		{
			Elements perMainClaEle = perMainEle.getElementsByAttributeValue("class", "_2s25 _606w");
			if(perMainClaEle.size() > 0 )
			{
				Element perMainAEle = perMainClaEle.get(0);
				String perMainHref = perMainAEle.attr("href");
				resultJSON.put("perMainHref", perMainHref);
			}
		}
		
		/*=========检查广告==========*/
		String wu = "无";
		String you = "有";
		String adver = "" ;
		Element adverEle = document.getElementById("ads_preferences_desktop_root");
		if(adverEle != null)
		{
			/* 木有广告 */
			adver = wu ; 
		}else
		{
			/* 有广告 */
			adver = you ; 
		}
		resultJSON.put("adver",adver);
		
		/* 是否包含主页 */
		String major = wu ; 
		resultJSON.put("major", major);
		String usersType = "" ; 
		/* 判断用户类型
		 * 大黑：无专页，无广告
		专业：有专页，无广告
		广告：有专页，有广告
		 *  */
		if(you.equalsIgnoreCase(major) && you.equalsIgnoreCase(adver))
		{
			usersType = "广告" ; 
		}else if(you.equalsIgnoreCase(major) && wu.equalsIgnoreCase(adver))
		{
			usersType = "专业" ; 
		}else if(wu.equalsIgnoreCase(major) && wu.equalsIgnoreCase(adver))
		{
			usersType = "大黑" ; 
		}else
		{
			usersType = "其它" ; 
		}
		resultJSON.put("usersType",usersType);
		
		
		/*=========截取字符串,然后再解析==========*/
		String beginStr = "<!--";
		String endStr = "-->" ; 
		Elements codesEles = document.getElementsByTag("code");
		for (Iterator iterator = codesEles.iterator(); iterator.hasNext();)
		{
			Element codeTemp = (Element) iterator.next();
			/* html字符串 */
			String htmlStr = codeTemp.html(); 
			if(htmlStr.indexOf(beginStr) != -1 && htmlStr.indexOf(endStr) != -1)
			{
				htmlStr = htmlStr.substring(htmlStr.indexOf(beginStr) + beginStr.length(), htmlStr.lastIndexOf(endStr));
			}
			/* 解析json字符串 */
			Document documentTemp = Jsoup.parse(htmlStr);
			/* 头部信息 */
			String headIndexStr = "id=\"pagelet_above_header_timeline\"" ;
			/* 基本信息 */
			String basicIndexStr = "data-overviewsection=\"contact_basic\"";
			if(htmlStr.indexOf(headIndexStr) != -1)
			{
				/* 处理头部信息 */
				/* 获取关于的链接 */
				Elements mainAboutAEles = documentTemp.getElementsByAttributeValue("data-tab-key", "about");
				if(mainAboutAEles.size() > 0 )
				{
					Element mainAboutAEle = mainAboutAEles.get(0);
					String aboutHref = mainAboutAEle.attr("href");
					resultJSON.put("aboutHref", aboutHref);
					//mainPageEleUlEle.getElementsByAttributeValue("data-gt", value);
				}
				
				/* 获取好友人数 */
				Elements friendsAEles = documentTemp.getElementsByAttributeValue("data-tab-key", "friends");
				if(friendsAEles.size() > 0 )
				{
					Element friendsAEle = friendsAEles.get(0);
					/* 提示信息 */
					Elements friendsSpanEles = friendsAEle.getElementsByTag("span");
					if(friendsSpanEles.size() > 0 )
					{
						Element friendsSpanEle = friendsSpanEles.get(0);
						String friendNum = friendsSpanEle.text() ; 
						/* 去掉,文字中的显示 */
						friendNum = friendNum.replaceAll("[,.]", "");
						resultJSON.put("friendNum", friendNum);
					}
					//mainPageEleUlEle.getElementsByAttributeValue("data-gt", value);
				}
			}else if(htmlStr.indexOf(basicIndexStr) != -1)
			{
				/* 基本信息 */
				/* 获取手机号 */
				Elements basicUlEles = documentTemp.getElementsByAttributeValue("data-overviewsection", "contact_basic");
				if(basicUlEles.size() > 0)
				{
					Element basicUlEle = basicUlEles.get(0);
					/* 基本信息 */
					Elements basicLiEles = basicUlEle.getElementsByTag("li");
					for (Iterator iterator2 = basicLiEles.iterator(); iterator2.hasNext();)
					{
						Element basicLiEleTemp = (Element) iterator2.next();
						/* 基本信息 */
						/* 手机号 */
						String htmlLiStr = basicLiEleTemp.html() ;
						
						/* 所有div的数量,包含孩子和孙子节点 */
						Elements basicLiDivEles = basicLiEleTemp.getElementsByTag("div");
						String lastNodeStr = "" ; 
						if(basicLiDivEles.size() > 0)
						{
							Element basicLiDivEle = basicLiDivEles.get(basicLiDivEles.size() - 1);
							lastNodeStr = basicLiDivEle.text().trim() ; 
						}
						
						/* 手机号 */
						String phone = "sx_2b7068" ; 
						/* 生日 */
						String birthday = "sx_336980" ; 
						if(htmlLiStr.indexOf(phone) != -1)
						{
							/* 手机号 */
							resultJSON.put("phone", lastNodeStr);
						}else if(htmlLiStr.indexOf(birthday) != -1)
						{
							/* 生日 */
							resultJSON.put("birthday", lastNodeStr);
						}
					}
				}
			}
		}
		return resultJSON ; 
	}
	
	/**
	 * 个人主页
	 * @return
	 */
	public JSONObject personRequest(String urlStr,String methodSou,JSONObject reqJSON)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====personRequest====");
		String method = "" ; 
		if(methodSou == null || "".equalsIgnoreCase(methodSou) )
		{
			method = "get";
		}
		/* 判断为空.然后再赋值 */
		method = methodSou;
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> dataOne = new HashMap<String, Object>();
		apiResponse.setDataOne(dataOne);
		/* 获取请求头和请求体 */
		JSONObject headerJSON = reqJSON.getJSONObject("header"); 
		JSONObject requestJSON = reqJSON.getJSONObject("request"); 
		try
		{
			/* ====请求头 ==== */
			headerJSON.putAll(headerCommonJSON);
			Map<String, String> headerMap = ConstatFinalUtil.jsonToMap(headerJSON);
			/* 生成json的语言 */
			headerMap.put(ConstatFinalUtil.REQ_HEADER_IGNORE + "domain", domain);
			/* 随机的存储Cookie和userAgent */
			ConstatFinalUtil.headerRandom(headerMap);
			//headerMap.put("", "");
			/* ====请求体 ==== */
			Map<String, String> paramsMap = ConstatFinalUtil.jsonToMap(requestJSON);
			/* ====响应体 ==== */
			Map<String, String> responseMap = new HashMap<String, String>();
			
			/* 请求网址 */
			String response = "";
			if("get".equalsIgnoreCase(method))
			{
				response = this.httpUtil.methodGet(urlStr, headerMap, paramsMap, responseMap);
			}else if("post".equalsIgnoreCase(method))
			{
				response = this.httpUtil.methodPost(urlStr, headerMap, paramsMap, responseMap);
			}
			//String response = this.httpUtil.broswerRequest(urlStr, "post", headerMap, paramsMap, responseMap);
			
			/* 把Map变成Json */
			JSONObject responseJSONMap = new JSONObject() ;
			responseJSONMap.putAll(responseMap);
			dataOne.put("responseMap", responseJSONMap);
			
			/* 将完整的请求头存储下来 */
			headerJSON.putAll(headerMap);
			requestJSON.putAll(paramsMap);
			
			/* Cookie最终的字符串 */
			String cookieTarget = responseMap.get("cookieTarget") ;
			dataOne.put("cookie", cookieTarget);
			/* 获取到Json字符串 */
			String cookieArr = responseMap.get("cookieArr");
			dataOne.put("cookieArr", cookieArr);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			
			/* 登陆以后的网址 */
			String location = responseJSONMap.getString("Location");
			if(location != null)
			{
				JSONObject resultJSON = this.accessUrl(responseJSONMap);
				/* 登陆以后的网址 */
				JSONObject dataResJSON = (JSONObject) resultJSON.get("data");
				response = dataResJSON.getString("response");
				
				/* Cookie最终的字符串 */
				cookieTarget = dataResJSON.getString("cookieTarget") ;
				if(cookieTarget != null && !"".equalsIgnoreCase(cookieTarget))
				{
					dataOne.put("cookie", cookieTarget);
					/* 获取到Json字符串 */
					cookieArr = dataResJSON.getString("cookieArr");
					dataOne.put("cookieArr", cookieArr);
				}
			}
			
			/* 解析网页上的信息 */
			JSONObject infoJSON = this.infoUtil(response);
			dataOne.put("infoJSON", infoJSON);
			
			dataOne.put("response", response);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("报错了",e);
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse.toJSON() ; 
	}
	
	/**
	 * 修改登陆页面
	 * @return
	 */
	public JSONObject accessUrl(JSONObject paramsJSON)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====accessUrl====");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> dataOne = new HashMap<String, Object>();
		apiResponse.setDataOne(dataOne);
		
		/* 请求网址 */
		String location = paramsJSON.getString("Location");
		if(location == null)
		{
			location = "" ; 
		}
		if(location.indexOf(ConstatFinalUtil.SPLIT_STR) != -1)
		{
			location = location.substring(0, location.indexOf(ConstatFinalUtil.SPLIT_STR));
		}
		/* ====请求头 ==== */
		JSONObject headerJSON = new JSONObject();
		headerJSON.putAll(headerCommonJSON);
		
		String cookie = paramsJSON.getString("cookieTarget") ; 
		/* 放置Cookie */
		headerJSON.put("cookie", cookie);
		Map<String, String> headerMap = ConstatFinalUtil.jsonToMap(headerJSON);
		/* 生成json的语言 */
		headerMap.put(ConstatFinalUtil.REQ_HEADER_IGNORE + "domain", domain);
		/* 随机的存储Cookie和userAgent */
		ConstatFinalUtil.headerRandom(headerMap);
		//headerMap.put("", "");
		/* ====请求体 ==== */
		JSONObject requestJSON = new JSONObject();
		Map<String, String> paramsMap = ConstatFinalUtil.jsonToMap(requestJSON);
		/* ====响应体 ==== */
		Map<String, String> responseMap = new HashMap<String, String>();
		
		/* 请求网址 */
		String response = this.httpUtil.methodGet(location, headerMap, paramsMap, responseMap);
		//String response = this.httpUtil.broswerRequest(location, "get", headerMap, paramsMap, responseMap);
		
		/* 将完整的请求头存储下来 */
		headerJSON.putAll(headerMap);
		requestJSON.putAll(paramsMap);
		
		/* 把Map变成Json */
		JSONObject responseJSONMap = new JSONObject() ;
		responseJSONMap.putAll(responseMap);
		dataOne.put("responseMap", responseJSONMap);
		
		/* 存储信息 */
		dataOne.put("cookie", responseMap.get("cookieTarget"));
		dataOne.put("cookieArr", responseMap.get("cookieArr"));
		dataOne.put("response", response);
		/* 解析网页上的信息 */
		JSONObject infoJSON = this.info(response);
		dataOne.put("infoJSON", infoJSON);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		
		/* 一定要让请求的网址不一样才可以,否则就是死循环 */
		location = responseJSONMap.getString("Location");
		if(location == null)
		{
			location = "" ; 
		}
		if(!"".equalsIgnoreCase(location))
		{
			/* 接着请求数据 */
			return this.accessUrl(responseJSONMap);
		}
		
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse.toJSON() ;
	}
	
	/**
	 * 修改登陆页面
	 * @return
	 */
	private boolean verifyLogin(String sourceHtml)
	{
		/* 获取html代码 */
		Document document = Jsoup.parse(sourceHtml);
		/* 获取表单 */
		Element loginFormEle = document.getElementById("login_form");
		Element pass = document.getElementById("pass");
		Element fbRequestsJewel = document.getElementById("fbRequestsJewel");
		
		if(loginFormEle == null && pass == null && fbRequestsJewel != null)
		{
			/* 登陆成功 */
			return true ; 
		}
		return false ; 
	}
	
	/**
	 * faceBook所有的流程走一遍
	 * 上行的参数包含:
	 * String email = reqJSON.getString("email");
	String phone = reqJSON.getString("phone");
	String password = reqJSON.getString("password");
	String ip = reqJSON.getString("ip");
	 * 		~获取登陆的Cookie
	 * @return
	 */
	public JSONObject all(JSONObject paramsJSON)
	{
		String email = paramsJSON.getString("email");
		String phone = paramsJSON.getString("phone");
		String password = paramsJSON.getString("password");
		/* 文档中存储的Cookie;Cookie存储的是JSON */
		String cookieSource = paramsJSON.getString("cookieSource");
		/* 最终想要的Cookie */
		String cookieTargetRes = ""; 
		
		JSONObject reqJSON = new JSONObject();
		/* 最终的结果 */
		JSONObject resultJSON = new JSONObject();
		/* 最终存储的数据 */
		JSONObject dataResultJSON = new JSONObject();
		/* 请求头 */
		JSONObject headerJSON = new JSONObject();
		/* 请求体 */
		JSONObject requestJSON = new JSONObject();
		/*=======请求首页,获取Cookie=========*/
		headerJSON.clear();
		/* 请求语言 */
		reqJSON.put("header", headerJSON);
		/* 请求体 */
		reqJSON.put("request", requestJSON);
		
		/* 所有的请求,返回的数据,都放到这里面 */
		JSONObject mainResJSON = new JSONObject();
		JSONObject mainDataResJSON = new JSONObject();
		/* 最终的网页内容 */
		String response = ""; 
		
		/* 先获取Ip地址;查询本机的Ip */
		Map<String, String> headerMap = new HashMap<String, String>();
		mainResJSON = this.jsoupUtil.ipLocation("", headerMap);
		String ip = "" ; 
		if(mainResJSON != null && mainResJSON.getString("code").equalsIgnoreCase(ApiResponseEnum.STATUS_SUCCESS.getStatus() + ""))
		{
			mainDataResJSON = mainResJSON.getJSONObject("data");
			ip = mainDataResJSON.getString("ip");
			dataResultJSON.put("ip", ip);
		}
		
		/* 个人主页的网址: */
		String perMainHref = "" ; 
		
		/*=======获取到有用的Cookie(和用户相关)=========*/
		if(cookieSource != null && !"".equalsIgnoreCase(cookieSource))
		{
			/* 文档中有Cookie,需要验证Cookie是否合法 */
			/* 获取cookieJSON数组 */
			JSONArray cookieArr = JSON.parseArray(cookieSource);
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < cookieArr.size(); i++)
			{
				JSONObject cookieTemp = cookieArr.getJSONObject(i);
				String name = cookieTemp.getString("name");
				String value = cookieTemp.getString("value");
				sb.append(name + "=" + value + ";");
			}
			String cookieTarget = sb.toString();
			/* 登陆后访问页面; */
			resultJSON = this.allMainAfterLoginUtil(cookieTarget, headerJSON);
		}else
		{
			/* 请求首页,目的是为了获取Cookie */
			mainResJSON = this.main(reqJSON);
			mainDataResJSON = (JSONObject) mainResJSON.get("data");
			if (mainDataResJSON == null)
			{
				/* 获取不到Cookie,继续 */
				resultJSON.put("code", ApiResponseEnum.INFO_COOKIE_NO.getStatus());
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.getString(resultJSON.getString("code")));
				return resultJSON ; 
			}
			
			/* 获取到Cookie */
			String cookieResponse = mainDataResJSON.getString("cookie");
			headerJSON.put("cookie", cookieResponse);
			
			/* 网页内容 */
			response = mainDataResJSON.getString("response");
			
			/* 获取网页上的有用信息 */
			JSONObject infoJSON = mainDataResJSON.getJSONObject("infoJSON");
			JSONObject loginJSON = infoJSON.getJSONObject("login");
			
			if(loginJSON != null)
			{
				String loginUrl = loginJSON.getString("action");
				requestJSON.clear();
				requestJSON.put("email", phone);
				requestJSON.put("pass", password);
				
				/* 将登陆页相关的其它参数加上去 */
				JSONArray paramsArr = loginJSON.getJSONArray("paramsArr");
				for (Iterator iterator = paramsArr.iterator(); iterator.hasNext();)
				{
					JSONObject paramsTempJSON = (JSONObject) iterator.next();
					String name = paramsTempJSON.getString("name");
					String value = paramsTempJSON.getString("value");
					requestJSON.put(name, value);
				}
				
				if(paramsArr.size() > 0 )
				{
				
					reqJSON.put("request", requestJSON);
					/* 快速休眠 */
					ConstatFinalUtil.randSleep("slow");
					// 这里必须再new一个地址 因为list是不会存内存地址一样的对象的
					mainResJSON = this.login(loginUrl,reqJSON);
					
					resultJSON.put("code", mainResJSON.getString("code"));
					resultJSON.put("info", mainResJSON.getString("info"));
					// 如果等于true代表访问faceBook成功了然后就给返回服务器的json添加数据
					/* ======所有的数据都上报服务器,便于后期人工来操作===== */
					/* 存储数据 */
					if (mainResJSON.getJSONObject("data") != null)
					{
						mainDataResJSON = mainResJSON.getJSONObject("data");
						String cookieUsers = mainDataResJSON.get("cookie")  + ""; 
						/* Cookie JSON,饼干插件使用 */
						String cookieFini = mainDataResJSON.get("cookieArr") + "";
						
						if(resultJSON.getString("code").equalsIgnoreCase(ApiResponseEnum.INFO_LOGIN_SUCCES.getStatus() + ""))
						{
							/* 登陆成功 */
							resultJSON = this.allMainAfterLoginUtil(cookieUsers, headerJSON);
							if(resultJSON.get("data") != null)
							{
								dataResultJSON = (JSONObject) resultJSON.get("data");
							}
						}
						
						/* 再存储一次 */
						dataResultJSON.put("cookieTarget", cookieUsers);
						dataResultJSON.put("cookieFini", cookieFini);
						dataResultJSON.put("usersId", mainDataResJSON.get("usersId"));
					}else
					{
						/* 为空 */
						dataResultJSON.put("cookieTarget", "");
						dataResultJSON.put("cookieFini", "");
						/* usersId */
						dataResultJSON.put("usersId", "");
					}
				}else
				{
					ConstatFinalUtil.SYS_LOGGER.error("登陆异常,木有获取到登陆信息;url:{}",loginUrl);
				}
				dataResultJSON.put("httpSource", headerJSON);
			}
		}
		/* 获取结果 */
		dataResultJSON = (JSONObject) resultJSON.get("data");
		if(dataResultJSON == null)
		{
			ConstatFinalUtil.SYS_LOGGER.error("数据为空,木有获取到数据:{}",resultJSON);
			return resultJSON ; 
		}
		/* 存储Ip */
		dataResultJSON.put("ip", ip);
		perMainHref = dataResultJSON.getString("perMainHref");
		/* ======获取最终的Cookie==== */
		cookieTargetRes = dataResultJSON.getString("cookieTarget");
		ConstatFinalUtil.SYS_LOGGER.info("最终的Cookie:{}",cookieTargetRes);
		if(cookieTargetRes != null && !"".equalsIgnoreCase(cookieTargetRes))
		{
			/* 可以获取到Cookie */
			/* 请求个人主页 */
			ConstatFinalUtil.SYS_LOGGER.info("perMainHref:{}",perMainHref);
			if(perMainHref != null && !"".equalsIgnoreCase(perMainHref))
			{
				/* 快速休眠 */
				ConstatFinalUtil.randSleep("slow");
				/* 关于的链接 */
				String aboutHref = "" ; 
				
				/* 登陆完成 */
				headerJSON.put("cookie", cookieTargetRes);
				reqJSON.put("header", headerJSON);
				/* 请求体 */
				JSONObject requestJSONCookie = new JSONObject();
				reqJSON.put("request", requestJSONCookie);
				/* 请求个人主页 */
				mainResJSON = this.personRequest(perMainHref, "get",reqJSON);
				/* 存储数据 */
				if (mainResJSON.getJSONObject("data") != null)
				{
					mainDataResJSON = mainResJSON.getJSONObject("data");
					/* 获取页面上有用的信息 */
					JSONObject infoJSON = (JSONObject) mainDataResJSON.get("infoJSON");
					
					/* 关于链接 */
					aboutHref = infoJSON.getString("aboutHref") ; 
					/* 存储关于我们的链接 */
					dataResultJSON.put("aboutHref", aboutHref);
					/* 存储关于我们的链接 */
					dataResultJSON.put("friendNum", infoJSON.getString("friendNum"));
					
					/* 网页内容 */
					response = mainDataResJSON.getString("response");
					dataResultJSON.put("response", response);
				}else
				{
					/* 存储关于我们的链接 */
					dataResultJSON.put("aboutHref", "");
					/* 存储关于我们的链接 */
					dataResultJSON.put("friendNum", "");
				}
				
				/* 请求关于的页面 */
				if(aboutHref != null && !"".equalsIgnoreCase(aboutHref))
				{
					/* 登陆完成 */
					headerJSON.put("cookie", cookieTargetRes);
					reqJSON.put("header", headerJSON);
					/* 请求体 */
					JSONObject requestAboutCookie = new JSONObject();
					reqJSON.put("request", requestAboutCookie);
					/* 请求个人主页 */
					mainResJSON = this.personRequest(aboutHref, "get",reqJSON);
					/* 存储数据 */
					if (mainResJSON.getJSONObject("data") != null)
					{
						mainDataResJSON = mainResJSON.getJSONObject("data");
						/* 获取页面上有用的信息 */
						JSONObject infoJSON = (JSONObject) mainDataResJSON.get("infoJSON");
						
						/* 生日 */
						dataResultJSON.put("birthday", infoJSON.getString("birthday"));
						/* 手机号 */
						dataResultJSON.put("phone", infoJSON.getString("phone"));
						
						/* 网页内容 */
						response = mainDataResJSON.getString("response");
						dataResultJSON.put("response", response);
					}else
					{
						/* 生日 */
						dataResultJSON.put("birthday", "");
						/* 手机号 */
						dataResultJSON.put("phone", "");
					}
				}
			}
			
			/* 一定要放到最后查询广告 */
			String urlStr = "https://www.facebook.com/ads/preferences/?entry_product=ad_settings_screen" ; 
			mainResJSON = this.personRequest(urlStr,"get", reqJSON);
			/* 存储数据 */
			if (mainResJSON.getJSONObject("data") != null)
			{
				mainDataResJSON = mainResJSON.getJSONObject("data");
				/* 获取页面上有用的信息 */
				JSONObject infoJSON = (JSONObject) mainDataResJSON.get("infoJSON");
				
				/* 存储结果 */
				dataResultJSON.put("major", infoJSON.get("major"));
				dataResultJSON.put("adver", infoJSON.get("adver"));
				dataResultJSON.put("usersType", infoJSON.get("usersType"));
			}else
			{
				/* 存储关于我们的链接 */
				dataResultJSON.put("major", "");
				dataResultJSON.put("adver", "");
				dataResultJSON.put("usersType", "");
			}
		}
		/* 存储网页内容 */
		dataResultJSON.put("response", response);
		/* 存储数据 */
		resultJSON.put("data", dataResultJSON);
		return resultJSON ; 
	}

	/**
	 * 验证登陆以后的Cookie
	 * @param reqJSON
	 */
	private JSONObject allMainAfterLoginUtil(String cookie,JSONObject headerJSON)
	{
		/* 存储结果的json */
		JSONObject resultJSON = new JSONObject();
		JSONObject dataResultJSON = new JSONObject();
		
		String perMainHref = "" ; 
		
		JSONObject reqJSON = new JSONObject();
		/* 登陆完成 */
		headerJSON.put("cookie", cookie);
		reqJSON.put("header", headerJSON);
		/* 请求体 */
		JSONObject requestJSONCookie = new JSONObject();
		reqJSON.put("request", requestJSONCookie);

		/* 快速休眠 */
		ConstatFinalUtil.randSleep("slow");
		/* 验证Cookie是否有效 */
		JSONObject mainResJSON = this.mainAfterLogin(reqJSON);
		/* 这个是结果 */
		resultJSON.put("code", mainResJSON.getString("code"));
		resultJSON.put("info", mainResJSON.getString("info"));
		
		/* 存储数据 */
		if (mainResJSON.getJSONObject("data") != null)
		{
			JSONObject mainDataResJSON = mainResJSON.getJSONObject("data");
			/* Cookie字符串 */
			dataResultJSON.put("cookieTarget", mainDataResJSON.get("cookie"));
			
			/* 网页内容 */
			String response = mainDataResJSON.getString("response");
			/* 获取页面上有用的信息 */
			JSONObject infoJSON = (JSONObject) mainDataResJSON.get("infoJSON");
			if(infoJSON != null)
			{
				perMainHref = infoJSON.getString("perMainHref");
			}
			dataResultJSON.put("response", response);
		}
		/* 存储个人首页的地址 */
		dataResultJSON.put("perMainHref", perMainHref);
		/* 存储请求头信息 */
		dataResultJSON.put("httpSource", headerJSON);
		/* 存储数据 */
		resultJSON.put("data", dataResultJSON);
		return resultJSON ; 
	}
}
